package com.wisedu.w5autocode;

import java.io.File;
import java.util.LinkedHashMap;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.IJavaProject;

public class MyEntityModel {

	private Boolean genPk = Boolean.FALSE;

	private String pkProperty;

	private Map<String, MyPropertyModel> properties = new LinkedHashMap<String, MyPropertyModel>();

	private String entityPackageName;

	private String basePackageName;

	private String modeName;

	private String entityClassName;

	private String webRoot;// eg:
							// E:/mywork/runtime-EclipseApplication/w3/web/WEB-INF

	private String root;

	private String projectName;

	private IJavaProject javaProject;

	private IProject project;

	public Boolean getGenPk() {
		return genPk;
	}

	public void setGenPk(Boolean genPk) {
		this.genPk = genPk;
	}

	public String getPkProperty() {
		return pkProperty;
	}

	public void setPkProperty(String pkProperty) {
		this.pkProperty = pkProperty;
	}

	public Map<String, MyPropertyModel> getProperties() {
		return properties;
	}

	public void setProperties(Map<String, MyPropertyModel> properties) {
		this.properties = properties;
	}

	public String getEntityPackageName() {
		return entityPackageName;
	}

	public void setEntityPackageName(String entityPackageName) {
		this.entityPackageName = entityPackageName;
	}

	public String getBasePackageName() {
		return basePackageName;
	}

	public void setBasePackageName(String basePackageName) {
		this.basePackageName = basePackageName;
	}

	public String getModeName() {
		return modeName;
	}

	public void setModeName(String modeName) {
		this.modeName = modeName;
	}

	public String getEntityClassName() {
		return entityClassName;
	}

	public void setEntityClassName(String entityClassName) {
		this.entityClassName = entityClassName;
	}

	public MyEntityModel() {
	}

	public String getWebRoot() {
		return webRoot;
	}

	public void setWebRoot(String webRoot) {
		this.webRoot = webRoot;
	}

	public MyEntityModel(ICompilationUnit compilationUnit) {
		setRoot(compilationUnit.getJavaProject().getResource().getProject().getLocation().toOSString());
		setWebRoot(getWebInf(getRoot()).replaceAll("\\\\", "/"));
		setProjectName(compilationUnit.getJavaProject().getResource().getProject().getName());
		setEntityClassName(StringUtils.removeEnd(compilationUnit.getElementName(), ".java"));
		setBasePackageName(compilationUnit.getParent().getElementName());
		setEntityPackageName(basePackageName + "." + entityClassName);
		setModeName(StringUtils.substringAfterLast(StringUtils.removeEnd(basePackageName, ".entity"), "."));
		setJavaProject(compilationUnit.getJavaProject());
		setProject(compilationUnit.getResource().getProject());
	}

	private String getWebInf(String root) {
		String webinfo = null;
		File f = new File(root);
		if (f.isDirectory()) {
			File[] files = f.listFiles();
			for (File file : files) {
				if (file.isDirectory()) {
					if (file.getName().equals("WEB-INF")) {
						webinfo = file.getPath();
						return webinfo;
					} else {
						webinfo = getWebInf(file.getPath());
					}
				}
			}
		}
		return webinfo;
	}

	public String getRoot() {
		return root;
	}

	public void setRoot(String root) {
		this.root = root;
	}

	public String getProjectName() {
		return projectName;
	}

	public void setProjectName(String projectName) {
		this.projectName = projectName;
	}

	public IJavaProject getJavaProject() {
		return javaProject;
	}

	public void setJavaProject(IJavaProject javaProject) {
		this.javaProject = javaProject;
	}

	public IProject getProject() {
		return project;
	}

	public void setProject(IProject project) {
		this.project = project;
	}

	public void refresh() {
		try {
			project.refreshLocal(IResource.DEPTH_INFINITE, null);
		} catch (CoreException e) {
			e.printStackTrace();
		}
	}

}
